package com.wen.apiinterfacemock.controller;



import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wen.apiclientsdk.model.User;
import com.wen.apiclientsdk.utils.SignUtil;
import com.wen.apiinterfacemock.service.JsonCompareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 查询名称接口
 */
@RestController
@RequestMapping("/name")
public class NameController {

    @Autowired
    private JsonCompareService jsonCompareService;

    @GetMapping("/get")
    public  String getNameByGet(@RequestParam String name){
        return "Get 你的名字是" + name;
    }
    @PostMapping("/post")
    public  String getNameByPost(@RequestParam String name){
        return "Post 你的名字是" + name;
    }
    @PostMapping("/user")
    public  String getNameByPost(@RequestBody User user, HttpServletRequest request){

        String result = "Post 用户名字是" + user.getName();
        //增加调用次数统计

        return result;
    }
    @PostMapping("/jsonCompare")
    public  String getNameByJsonCompare(@RequestBody String jsonA, @RequestBody String jsonB){
        ObjectMapper mapper = new ObjectMapper();

        try {
           JsonNode jsonNode =  jsonCompareService.compare(mapper.readTree(jsonA),mapper.readTree(jsonB));
           return jsonNode.toString();
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}
